﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Net;
using Newtonsoft.Json;

namespace LoL_Profiler
{
    public partial class frmAccInf : Form
    {
        public frmAccInf()
        {
            InitializeComponent();
        }

        readonly static string key = "pGoKJiZ9YFfZO9EzYYiY";

        private void frmAccInf_Load(object sender, EventArgs e)
        {
            if (File.Exists(Application.StartupPath + @"\SumNames.dat"))
            {
                using (StreamReader reader = new StreamReader(Application.StartupPath + @"\SumNames.dat"))
                {
                    do
                    {
                        txtExistSumName.Items.Add(reader.ReadLine());
                    }
                    while (reader.EndOfStream != true);
                    txtNewSumName.Text = txtExistSumName.Items[txtExistSumName.Items.Count - 1].ToString();
                }
            }
        }

        private void btnNewSum_Click(object sender, EventArgs e)
        {
            if (txtExistSumName.Items.Contains(txtNewSumName.Text) == false)
            {
                string temp = null;
                if (File.Exists(Application.StartupPath + @"\SumNames.dat"))
                {
                    using (StreamReader reader = new StreamReader(Application.StartupPath + @"\SumNames.dat"))
                    {
                        temp = reader.ReadToEnd();
                    }
                }
                using (StreamWriter writer = new StreamWriter(Application.StartupPath + @"\SumNames.dat"))
                {
                    if (temp != "")
                    {
                        writer.Write(temp);
                    }
                    writer.WriteLine(txtNewSumName.Text);
                }
            }
            /*
            catch
            {
                MessageBox.Show("The Summoner Name doesn't exist or the servers are down...");
            }
             * */
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {                     
                // To make it not download the data everytime you want another part of the same data
                ElophantAPI.Profile.Data dataProfile = new ElophantAPI.Profile.Data();
                dataProfile = ElophantAPI.Profile.getProfileSummonerInfo(client.DownloadString(String.Format(@"http://api.elophant.com/v2/{0}/summoner/{1}?key={2}", txtRegion.Text.ToLower(), txtNewSumName.Text.ToLower(), key))).data;

                ElophantAPI.PlayerStats.Data dataStats = new ElophantAPI.PlayerStats.Data();
                dataStats = ElophantAPI.PlayerStats.getPlayerStats(client.DownloadString(String.Format(@"http://api.elophant.com/v2/{0}/player_stats/{1}/current?key={2}", txtRegion.Text, dataProfile.acctId, key))).data;

                ElophantAPI.RecentGames.Data dataGames = new ElophantAPI.RecentGames.Data();
                dataGames = ElophantAPI.RecentGames.getRecentGames(client.DownloadString(String.Format("http://api.elophant.com/v2/{0}/recent_games/{1}?key={2}", txtRegion.Text, dataProfile.acctId, key))).data;

                List<Match> matches = new List<Match>();
                foreach (ElophantAPI.RecentGames.GameStatistic t in dataGames.gameStatistics)
                {
                    Match m = new Match();
                    foreach (ElophantAPI.RecentGames.Statistic stats in t.statistics)
                    {
                        switch (stats.statType)
                        {
                            case "CHAMPIONS_KILLED":
                                m.ownKills = stats.value;
                                break;
                            case "ASSISTS":
                                m.ownAssists = stats.value;
                                break;
                            case "NUM_DEATHS":
                                m.ownDeaths = stats.value;
                                break;
                            case "WIN":
                                m.Outcome = Convert.ToBoolean(Convert.ToInt16(stats.value));
                                break;
                            default:
                                break;
                        }                        
                    }
                    m.Type = t.subType;
                    MessageBox.Show(t.gameId.ToString());
                    MessageBox.Show(t.teamId.ToString());
                    m.teamId = t.teamId.ToString();
                    m.gameId = t.gameId.ToString();
                    List<Teammate> teammates = new List<Teammate>();
                    foreach (ElophantAPI.RecentGames.FellowPlayer teammate in t.fellowPlayers)
                    {
                        Teammate mate = new Teammate();
                        mate.SummonerID = teammate.summonerId.ToString();
                        mate.SummonerName = teammate.summonerName;
                        teammates.Add(mate);
                    }                    
                    m.Team = teammates;                    
                    matches.Add(m);
                }         
                
                Summoner ikzelf = new Summoner() { SummonerID = dataProfile.summonerId.ToString(), SummonerName = dataProfile.name, Region = txtRegion.Text }; ;
                ikzelf.Elo = dataStats.playerStatSummaries.playerStatSummarySet[0].rating;
                ikzelf.MatchHistory = matches;
                
                List<Summoner> test123 = new List<Summoner>();
                test123.Add(ikzelf);
                XMLParser.writeToXML(test123);
            }
        }
    }
}
